import ace from 'ace-builds'
import  _ from 'lodash'
export default class SourceView {
  constructor(args){
    this._options = args;

    this._container = _.get(args, 'sourceContainer');
    this._mainEditor = new SiddhiEditor({
      divID: args.sourceContainer,
      realTimeValidation: true,
      autoCompletion: true
    });
    this._editor = ace.edit(this._container);
    this._mainEditor.setContent(args.source);
    this._app = _.get(args, 'app');
    this._storage = this._app.browserStorage;
    }


  render(options){
      var self = this;
      $(this._container).show();
      var editorFontSize = (this._storage.get("pref:sourceViewFontSize") != null) ?
          this._storage.get("pref:sourceViewFontSize") : _.get(this._options, 'font_size');
      var editorTheme = (this._storage.get("pref:sourceViewTheme") != null) ? this._storage.get
      ("pref:sourceViewTheme") : _.get(this._options, 'theme');

      self._editor.setFontSize(editorFontSize);
      self._editor.setTheme(editorTheme);

      this._editor.on("change", function (event) {
          if (!self._inSilentMode) {
              var changeEvent = {
                  type: "source-modified",
                  title: "Modify source",
                  data: {
                      type: event.action,
                      lines: event.lines
                  }
              };
              self.trigger('modified', changeEvent);
          }
      });

  };

  editorResize (){
      var self = this;
      self._editor.resize(true);
  };

  setFontSize (size){
      var self = this;
      self._editor.setFontSize(size);
  };

  setTheme (theme){
      var self = this;
      self._editor.setTheme(theme);
  };

  setContent (content){
      this._inSilentMode = true;
      this._editor.session.setValue(content);
      this._inSilentMode = false;
      this.markClean();
  };

  getContent (){
      return this._editor.session.getValue();
  };

  getEditor (){
      return this._editor;
  };

  getDebugger (){
      return this._mainEditor.getDebugger();
  };

  getRawExtensions (){
      return this._mainEditor.getRawExtensions();
  };

  onEnvironmentChange (){
      this._mainEditor.resetValidations();
  };

  bindCommand (command){
      var id = command.id,
          hasShortcut = _.has(command, 'shortcuts'),
          self = this;
      if (hasShortcut) {
          var macShortcut = _.replace(command.shortcuts.mac.key, '+', "-"),
              winShortcut = _.replace(command.shortcuts.other.key, '+', "-");
          this.getEditor().commands.addCommand({
              name: id,
              bindKey: {win: winShortcut, mac: macShortcut},
              exec: function (editor) {
                  self.trigger('dispatch-command', id);
              }
          });
      }
  };

  show (){
      $(this._container).show();
  };

  hide (){
      $(this._container).hide();
  };

  isVisible (){
      return $("#" + this._container).is(':visible');
  };

  format (doSilently){
      if (doSilently){
          this._inSilentMode = true;
      }
      beautify.beautify(this._editor.session);
      if (doSilently){
          this._inSilentMode = false;
          this.markClean();
      }
  };

  isClean(){
      return this._editor.getSession().getUndoManager().isClean();
  };

  undo(){
      return this._editor.getSession().getUndoManager().undo();
  };

  redo(){
      return this._editor.getSession().getUndoManager().redo();
  };

  markClean(){
      this._editor.getSession().getUndoManager().markClean();
  };

  getMainEditor(){
      return this._mainEditor;
  };
}
